package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.DataScreeningPersonnel;
import com.ruoyi.system.domain.DataScreeningPersonnelUav;
import com.ruoyi.system.domain.SysOrderP;
import com.ruoyi.system.mapper.DataScreeningPersonnelMapper;
import com.ruoyi.system.mapper.DataScreeningPersonnelUavMapper;
import com.ruoyi.system.mapper.SysOrderPMapper;
import com.ruoyi.system.service.IDataScreeningPersonnelService;
import com.ruoyi.system.vo.req.order.OrderStrikeRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DataScreeningPersonnelControlMapper;
import com.ruoyi.system.domain.DataScreeningPersonnelControl;
import com.ruoyi.system.service.IDataScreeningPersonnelControlService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 管控情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-25
 */
@Service
public class DataScreeningPersonnelControlServiceImpl extends ServiceImpl<DataScreeningPersonnelControlMapper, DataScreeningPersonnelControl> implements IDataScreeningPersonnelControlService
{
    @Autowired
    private DataScreeningPersonnelControlMapper dataScreeningPersonnelControlMapper;

    @Autowired
    private DataScreeningPersonnelMapper dataScreeningPersonnelService;


    @Autowired
    private DataScreeningPersonnelUavMapper dataScreeningPersonnelUavMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询管控情况
     * 
     * @param controlId 管控情况主键
     * @return 管控情况
     */
    @Override
    public DataScreeningPersonnelControl selectDataScreeningPersonnelControlByControlId(Long controlId)
    {
        return dataScreeningPersonnelControlMapper.selectDataScreeningPersonnelControlByControlId(controlId);
    }

    /**
     * 查询管控情况列表
     * 
     * @param dataScreeningPersonnelControl 管控情况
     * @return 管控情况
     */
    @Override
    public List<DataScreeningPersonnelControl> selectDataScreeningPersonnelControlList(DataScreeningPersonnelControl dataScreeningPersonnelControl)
    {
        return dataScreeningPersonnelControlMapper.selectDataScreeningPersonnelControlList(dataScreeningPersonnelControl);
    }

    /**
     * 新增管控情况
     * 
     * @param dataScreeningPersonnelControl 管控情况
     * @return 结果
     */
    @Override
    public int insertDataScreeningPersonnelControl(DataScreeningPersonnelControl dataScreeningPersonnelControl)
    {
        dataScreeningPersonnelControl.setCreateTime(DateUtils.getNowDate());
        return dataScreeningPersonnelControlMapper.insertDataScreeningPersonnelControl(dataScreeningPersonnelControl);
    }

    /**
     * 修改管控情况
     * 
     * @param dataScreeningPersonnelControl 管控情况
     * @return 结果
     */
    @Override
    public int updateDataScreeningPersonnelControl(DataScreeningPersonnelControl dataScreeningPersonnelControl)
    {
        dataScreeningPersonnelControl.setUpdateTime(DateUtils.getNowDate());
        return dataScreeningPersonnelControlMapper.updateDataScreeningPersonnelControl(dataScreeningPersonnelControl);
    }

    /**
     * 批量删除管控情况
     * 
     * @param controlIds 需要删除的管控情况主键
     * @return 结果
     */
    @Override
    public int deleteDataScreeningPersonnelControlByControlIds(Long[] controlIds)
    {
        return dataScreeningPersonnelControlMapper.deleteDataScreeningPersonnelControlByControlIds(controlIds);
    }

    /**
     * 删除管控情况信息
     * 
     * @param controlId 管控情况主键
     * @return 结果
     */
    @Override
    public int deleteDataScreeningPersonnelControlByControlId(Long controlId)
    {
        return dataScreeningPersonnelControlMapper.deleteDataScreeningPersonnelControlByControlId(controlId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HashMap exportGK(OrderStrikeRequestVO req) {



        HashMap<String, Integer> outmap = new HashMap<>();

        if(req.getIsVaild() == 0){
            return outmap;
        }

        Long userId = tokenService.getLoginUser().getSysUser().getUserId();
        Date date = new Date();

        //查询管控库中是否存在 按照身份证
        OrderStrikeRequestVO.ValidUserInfo validUserInfo = req.getValidUserInfo();

        OrderStrikeRequestVO.OrderStrikeInfo orderStrikeInfo = req.getOrderStrikeInfo();
        DataScreeningPersonnel one = dataScreeningPersonnelService.selectOne(new LambdaQueryWrapper<DataScreeningPersonnel>()
                .eq(DataScreeningPersonnel::getStatus, 0).eq(DataScreeningPersonnel::getIdentityCard, validUserInfo.getOrderStrikeUserIndo()));
        if(one!=null){
            one.setStatus(1);
            //先删后增
            dataScreeningPersonnelService.updateById(one);
        }
        //人员数据导入
        DataScreeningPersonnel dataScreeningPersonnel = new DataScreeningPersonnel();
        dataScreeningPersonnel.setScreeningPersonnelName(validUserInfo.getOrderStrikeUserName());
        dataScreeningPersonnel.setCurrentResidentialAddress(validUserInfo.getOrderStrikeUserAddress());
        dataScreeningPersonnel.setWorkUnit(validUserInfo.getOrderStrikeUserWork());
        dataScreeningPersonnel.setJobOccupation(validUserInfo.getOrderStrikeUserOccupation());
        dataScreeningPersonnel.setIdentityCard(validUserInfo.getOrderStrikeUserIndo());
        dataScreeningPersonnel.setPhone(validUserInfo.getOrderStrikeUserTel());
        dataScreeningPersonnel.setDeptId(orderStrikeInfo.getChangPoliceId().longValue());
        dataScreeningPersonnel.setLocalPoliceStationId(null);
        dataScreeningPersonnel.setLocalPoliceStation(orderStrikeInfo.getCensusPoliceId());
        dataScreeningPersonnel.setPolicyAdvocacy(orderStrikeInfo.getXjFlag());
        dataScreeningPersonnel.setWhetherHarbin(0);
        dataScreeningPersonnel.setPersonnelType(0);
        //默认身份证为12345678910为重点人员
        if(validUserInfo.getOrderStrikeUserIndo().equals("12345678910")){
            dataScreeningPersonnel.setPersonnelType(1);
        }
        dataScreeningPersonnel.setControlType(2);
        dataScreeningPersonnel.setStatus(0);
        dataScreeningPersonnel.setCreateUser(userId);
        dataScreeningPersonnel.setUpdateUser(userId);

        dataScreeningPersonnel.setCreateTime(date);
        dataScreeningPersonnel.setUpdateTime(date);
        dataScreeningPersonnel.setRemark(orderStrikeInfo.getRemark());
        dataScreeningPersonnelService.insertDataScreeningPersonnel(dataScreeningPersonnel);
        //无人机数据导入
        if(!req.getValidDroneInfo().isEmpty()){
            for (OrderStrikeRequestVO.ValidDroneInfo validDroneInfo : req.getValidDroneInfo()) {
                DataScreeningPersonnelUav dataScreeningPersonnelUav = new DataScreeningPersonnelUav();
                dataScreeningPersonnelUav.setScreeningPersonnelId(dataScreeningPersonnel.getScreeningPersonnelId());
                dataScreeningPersonnelUav.setDroneBrand(validDroneInfo.getDroneBrand());
                dataScreeningPersonnelUav.setUavModel(validDroneInfo.getDroneModel());
                dataScreeningPersonnelUav.setDroneIdentificationCode(validDroneInfo.getDroneCode());
                dataScreeningPersonnelUav.setPurpose(validDroneInfo.getDronePurpose());
                dataScreeningPersonnelUav.setUseType(validDroneInfo.getDroneUse());
                dataScreeningPersonnelUav.setStatus(0);
                dataScreeningPersonnelUav.setRemark(validDroneInfo.getDroneRemark());
                dataScreeningPersonnelUav.setCreateUser(userId);
                dataScreeningPersonnelUav.setUpdateUser(userId);
                dataScreeningPersonnelUav.setCreateTime(date);
                dataScreeningPersonnelUav.setUpdateTime(date);
                dataScreeningPersonnelUavMapper.insertDataScreeningPersonnelUav(dataScreeningPersonnelUav);

            }
        }

        outmap.put("screeningPersonnelId",dataScreeningPersonnel.getScreeningPersonnelId().intValue());


        return outmap;
    }
}
